#!/bin/bash

if [ -f /root/workspace/src/httpserver/httpserver.bin ]
then
  cd /root/workspace/src/httpserver/
  /root/workspace/src/httpserver/httpserver.bin
else
  echo 'export GOROOT=/usr/local/go' >> /root/.bashrc
  echo 'export GOPATH=/root/workspace' >> /root/.bashrc
  echo 'export GOBIN=$GOPATH/bin' >> /root/.bashrc
  echo 'export PATH=$PATH:$GOBIN:$GOROOT/bin' >> /root/.bashrc
  echo 'export GOPROXY=https://goproxy.cn' >> /root/.bashrc

  source /root/.bashrc

  mkdir -p /root/workspace
  mkdir -p $GOPATH/{pkg,bin,src}

  echo
  go version

  echo -e "\nGo init done\n"

  tar -zxf /root/httpserver.tar.gz -C $GOPATH/src/

  cd /root/workspace/src/httpserver/
  source /root/.bashrc
  go build -o /root/workspace/src/httpserver/httpserver.bin /root/workspace/src/httpserver/main.go

  echo -e "\nhttpserver building completed\n"

  cd /root/workspace/src/httpserver/
  source /root/.bashrc
  /root/workspace/src/httpserver/httpserver.bin

fi